home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-10-28 | 5.5 KB | 208 lines | [TEXT/CWIE] |
- #include <stdio.h>
- #include <SIOUX.h>
-
- #include <AEPrint.h>
-
- #include <LPreferencesFile.h>
- #include <UEventUtils.h>
- #include <UResourceMgr.h>
-
- #include "StNotificationRecord.h"
- #include "StPPCBrowser.h"
- #include "StSpinningCursor.h"
- #include "UAppleEvents.h"
- #include "UProcesses.h"
-
- #include "CAERepeater.h"
- #include "CLauncherWindow.h"
- #include "RheaConstants.h"
- #include "SlurpeeCreators.h"
-
- // static member variables
- Boolean CAERepeater::sDebuggingEnabled = false;
-
- CAERepeater::CAERepeater(LModelObject* inDefaultModel)
- : LModelDirector(inDefaultModel)
- {
- // install a generic handler
- ThrowIfOSErr_(::AEInstallEventHandler(typeWildCard, typeWildCard,
- NewAEEventHandlerProc(LModelDirector::AppleEventHandler),
- 0, false));
-
- // install recording handlers
- ThrowIfOSErr_(UAppleEvents::InitRecordability());
-
- // set-up SIOUX
- SIOUXSetTitle("\pDebug Window");
- SIOUXSettings.initializeTB = false;
- SIOUXSettings.standalone = false;
- SIOUXSettings.setupmenus = false;
- SIOUXSettings.autocloseonquit = true;
- SIOUXSettings.asktosaveonclose = false;
- SIOUXSettings.showstatusline = false;
- SIOUXSettings.toppixel = 0;
- SIOUXSettings.leftpixel = 0;
-
- // check for the other three finger salute
- EventModifiers lModifiers = UEventUtils::GetModifiers();
- if ((lModifiers & controlKey) && (lModifiers & optionKey) && (lModifiers & cmdKey))
- {
- sDebuggingEnabled = true;
- printf("Debugging enabled\n");
- }
- }
-
- CAERepeater::~CAERepeater(void)
- {
- }
-
- void CAERepeater::HandleAppleEvent(const AppleEvent& inAppleEvent, AppleEvent& outReply, Int32 inRefCon)
- {
- PrintAEDesc(inAppleEvent);
-
- switch(inRefCon)
- {
- case ae_OpenApp:
- case ae_Quit:
- {
- // these are special, just pass them on to the default model (the application)
- StAEDescriptor lDummyResult;
-
- LModelObject::GetDefaultModel()->HandleAppleEvent(inAppleEvent, outReply, lDummyResult, inRefCon);
- }
- break;
-
- default:
- {
- // whack up a wait cursor
- StSpinningBeachBallCursor lBeachBall;
-
- // get the original sender
- StAEDescriptor lSender;
- ThrowIfOSErr_(::AEGetAttributeDesc(&inAppleEvent, keyOriginalAddressAttr, typeWildCard, lSender));
-
- // create the address descriptor
- StAEDescriptor lTargetAddress;
- ThrowIfOSErr_(::AECreateDesc(typeTargetID,
- &(CLauncherWindow::GetConnection()->GetTargetID()),
- sizeof(TargetID), lTargetAddress));
-
- // duplicate the event and change address
- StAEDescriptor lAppleEvent;
- ThrowIfOSErr_(::AEDuplicateDesc(&inAppleEvent, lAppleEvent));
- ThrowIfOSErr_(::AEPutAttributeDesc(lAppleEvent, keyAddressAttr, lTargetAddress));
-
- // get the timeout if it exists
- long lTimeout = kAEDefaultTimeout;
- {
- StAEDescriptor lTimeoutDesc;
- if (::AEGetAttributeDesc(&inAppleEvent, keyTimeoutAttr, typeWildCard, lTimeoutDesc) == noErr)
- {
- UExtractFromAEDesc::TheInt32(lTimeoutDesc, lTimeout);
- }
- }
-
- // send the apple event, waiting for the reply
- StAEDescriptor lReply;
- OSErr lSendErr = UAppleEvents::SendWaitReply(lAppleEvent, lReply, lTimeout);
-
- // send the apple event to ourselves for recording
- UAppleEvents::Record(inAppleEvent);
-
- // duplicate the reply and re-address
- ThrowIfOSErr_(::AEDuplicateDesc(lReply, &outReply));
- ThrowIfOSErr_(::AEPutAttributeDesc(&outReply, keyAddressAttr, lSender));
-
- ThrowIfOSErr_(lSendErr);
- }
- break;
- }
-
- PrintAEDesc(outReply);
- }
-
- void CAERepeater::OpenConnection(TargetID& outTarget)
- {
- // create a notification record for use with AEInteractWithUser
- StNotificationRecord lNotificationRecord;
-
- // attempt to get the last target address from a preference file
- LStr255 lPrefFileName(ResID_ConfigStrings, configStrings_PrefFileName);
- LPreferencesFile lPreferences(lPrefFileName);
- lPreferences.OpenOrCreateResourceFork(fsRdWrPerm, sig_Rhea, fileType_PrefFile, smSystemScript);
- Boolean lDefaultSpecified = false;
- {
- StResource lResource(typeTargetID, ResID_TargetPreference, false, true);
- if (lResource.mResourceH != nil)
- {
- ::BlockMoveData(*lResource.mResourceH, &outTarget, sizeof(TargetID));
- lDefaultSpecified = true;
- }
- }
-
- // browse
- SFTypeList lTypes;
- lTypes[0] = sig_Sandra;
- LStr255 lPrompt(ResID_ConfigStrings, configStrings_BrowserPrompt);
- LStr255 lAppLabel(ResID_ConfigStrings, configStrings_BrowserAppLabel);
-
- // force this application to the front
- ProcessSerialNumber lThisProcess;
- if (::GetCurrentProcess(&lThisProcess) == noErr)
- {
- UProcesses::BringToFront(lThisProcess);
- }
-
- UDesktop::Deactivate();
- ThrowIfOSErr_(UAppleEvents::InteractWithUser(lNotificationRecord));
- StPPCBrowser lBrowser(1, lTypes, outTarget, lDefaultSpecified, true, lPrompt, lAppLabel);
- UDesktop::Activate();
-
- // whack up a wait cursor
- StSpinningBeachBallCursor lBeachBall;
-
- // save the target ID into the preference file
- {
- StNewResource lResource(typeTargetID, ResID_TargetPreference, sizeof(TargetID), true);
- ::BlockMoveData(&outTarget, *lResource.mResourceH, sizeof(TargetID));
- }
- }
-
- /* static */
- CAERepeater* CAERepeater::GetAERepeater(void)
- {
- return (CAERepeater*)sModelDirector;
- }
-
- /* static */
- void CAERepeater::PrintAEDesc(const AEDesc& inAEDesc)
- {
- if (sDebuggingEnabled)
- {
- char* lBuffer = nil;
- try
- {
- long lSize;
-
- ThrowIfOSErr_(::AEPrintSize((AEDesc*)&inAEDesc, &lSize));
-
- ThrowIfMemFail_(lBuffer = (char*)::NewPtr(lSize + 1));
-
- ThrowIfOSErr_(::AEPrint((AEDesc*)&inAEDesc, lBuffer, lSize));
-
- printf("--\n%s\n", lBuffer);
-
- ::DisposePtr(lBuffer);
- }
- catch(ExceptionCode inErr)
- {
- if (lBuffer != nil)
- {
- ::DisposePtr(lBuffer);
- }
-
- printf("Unable to write debug info : %d\n", inErr);
- }
- }
- }
-